package com.example.finalapplication;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;


public class MainActivity1 extends AppCompatActivity {
        private static final String TAG = "MainActivity1";
        TextView out;
        EditText input;
        EditText input1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main1);

            out = findViewById(R.id.txtout);
            input = findViewById(R.id.inp);
            input1 = findViewById(R.id.inp1);

            findViewById(R.id.btn1).setOnClickListener(this::onClick);
            findViewById(R.id.btn2).setOnClickListener(this::onClick);

        }

        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn1:
                    String s1 = input.getText().toString();
                    String s2 = input1.getText().toString();
                    String s3 = String.valueOf(Double.parseDouble(s2) * Double.parseDouble(s2));
                    String s4 = String.format("%.2f", Double.parseDouble(s1) / Double.parseDouble(s3));
                    double x = Double.parseDouble(s4);
                    if (x < 18.5) {
                        String s5 = "偏瘦，请适当增加饮食";
                        out.setText("您的健康指数为：" + s4 + "\n对您的健康建议为：" + s5);
                    } else if (x <= 25 && x >= 18.5) {
                        String s5 = "健康体型，请保持";
                        out.setText("您的健康指数为：" + s4 + "\n对您的健康建议为：" + s5);
                    } else if (x <= 28 && x > 25) {
                        String s5 = "超重，请适当减少饮食";
                        out.setText("您的健康指数为：" + s4 + "\n对您的健康建议为：" + s5);
                    } else if (x > 28) {
                        String s5 = "肥胖，请减少饮食或及时就医";
                        out.setText("您的健康指数为：" + s4 + "\n对您的健康建议为：" + s5);
                    }
                    break;
                case R.id.btn2:
                    String text = out.getText().toString();
                    Intent intent = new Intent(this,HomeActivity.class);
                    intent.putExtra("input_text",text);
                    startActivity(intent);
                    break;
            }
        }
    }

